import { Injectable } from "@angular/core";
import { UnlockService } from "../native/authentication/authentication.service";

@Injectable()
export class CaptchaIndentationService {
    private modal: HTMLElement;
    private captcha: any;

    constructor(private unlockService: UnlockService) {
        this.modal = null;
    }

    public init(element: HTMLElement) {
        if (typeof element === "undefined" || element == null) {
            return;
        }
        this.modal = element;
        this.modal.hidden = true;
        // this.createContainer();
        this.initCaptcha();

    }

    public show() {
        if (this.modal == null) {
            return;
        }
        this.modal.hidden = false;
        this.captcha.reset();
    }

    public hide() {
        if (this.modal == null) {
            return;
        }
        this.modal.hidden = true;
    }

    public initCaptcha() {
        const verifySig = this.unlockService.verifyCaptchaSignature;
        var nc_token = ["CF_APP_1", (new Date()).getTime(), Math.random()].join(':');
        var NC_Opt = {
            renderTo: "#captcha",
            appkey: "CF_APP_1",
            scene: 'test',
            token: nc_token,
            trans: {'key1': "code0"},
            is_Opt: 0,
            type: "scrape",
            width: 200,
            height: 80,
            isEnabled: true,
            language: 'cn',
            times: 5,
            objects: ["//img.alicdn.com/tps/TB1BT9jPFXXXXbyXFXXXXXXXXXX-80-80.png"],//勿动，照抄即可
            apimap: {
                // 'uab_Url': '//aeu.alicdn.com/js/uac/909.js',
            },
            elements: [
                '//img.alicdn.com/tfs/TB17cwllsLJ8KJjy0FnXXcFDpXa-50-74.png',
                '//img.alicdn.com/tfs/TB17cwllsLJ8KJjy0FnXXcFDpXa-50-74.png'
            ],
            bg_back_prepared: '//img.alicdn.com/tps/TB1skE5SFXXXXb3XXXXXXXXXXXX-100-80.png',
            bg_front: '',
            obj_ok: '//img.alicdn.com/tfs/TB1rmyTltfJ8KJjy0FeXXXKEXXa-50-74.png',
            bg_back_pass: '//img.alicdn.com/tfs/TB1KDxCSVXXXXasXFXXXXXXXXXX-100-80.png',
            obj_error: '//img.alicdn.com/tfs/TB1q9yTltfJ8KJjy0FeXXXKEXXa-50-74.png',
            bg_back_fail: '//img.alicdn.com/tfs/TB1w2oOSFXXXXb4XpXXXXXXXXXX-100-80.png',
            upLang:{"cn":{
                    _ggk_guide: "请在屏幕上滑动，刮出两面盾牌",
                    _ggk_success: "恭喜您成功刮出盾牌<br/>继续下一步操作吧",
                    _ggk_loading: "加载中",
                    _ggk_fail: ['呀，盾牌不见了<br/>请', "javascript:NoCaptcha.reset()", '再来一次'],
                    _ggk_action_timeout: ['我等得太久啦<br/>请', "javascript:NoCaptcha.reset()", '再来一次'],
                    _ggk_net_err: ['网络实在不给力<br/>请', "javascript:NoCaptcha.reset()", '再来一次'],
                    _ggk_too_fast: ['您刮得太快啦<br/>请', "javascript:NoCaptcha.reset()", '再来一次']
                }
            },
            callback: function(data) { //成功回调
                console.info(data)
            },
            failCallback: function(data) { //拦截or失败回调
                console.info(data)
            },
            error: function(data) { //异常回调
                console.info(data)
            }
        };
        let noCAPTCHA = window['NoCaptcha'];
        var nc = noCAPTCHA.init(NC_Opt);
        nc.setEnabled(true); // 启动
    }
}
